﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Security;

namespace SPBusiness
{
   public sealed class DocumentLibrary
    {
       /// <summary>
       /// DocumentLibrary class has only Static Methods, no constructor needed.
       /// </summary>
       private DocumentLibrary() { }
        public static void PublishAndApproveFilesRecursive(SPDocumentLibrary vSPDocumentLibrary)
        {
            try
            {

                if (vSPDocumentLibrary != null)
                {
                    if (vSPDocumentLibrary.EnableVersioning)
                    {
                        if (vSPDocumentLibrary.CheckedOutFiles != null)
                        {
                            foreach (SPCheckedOutFile vSPCheckedOutFile in vSPDocumentLibrary.CheckedOutFiles)
                            {
                                vSPCheckedOutFile.TakeOverCheckOut();
                                SPListItem vSPListItem = vSPDocumentLibrary.GetItemById(vSPCheckedOutFile.ListItemId);
                                File.Approve(vSPListItem.File);
                                //vSPListItem.File.CheckIn(string.Empty);
                            }
                        }

                        if (vSPDocumentLibrary.RootFolder != null)
                        {
                            Folder.PublishAndApproveFilesRecursive(vSPDocumentLibrary.RootFolder);
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(DocumentLibrary).Namespace, typeof(DocumentLibrary).Name, "PublishAndApproveFilesRecursive", ex);
                throw new Exception(ex.Message + Environment.NewLine + "em " + vSPDocumentLibrary.Title, ex);
            }
        }

        public static void ChekInFilesRecursive(SPDocumentLibrary vSPDocumentLibrary, SPCheckinType pSPCheckinType)
        {
            try
            {

                if (vSPDocumentLibrary.EnableVersioning)
                {
                    foreach (SPCheckedOutFile vSPCheckedOutFile in vSPDocumentLibrary.CheckedOutFiles)
                    {
                        vSPCheckedOutFile.TakeOverCheckOut();
                        SPListItem vSPListItem = vSPDocumentLibrary.GetItemById(vSPCheckedOutFile.ListItemId);
                        vSPListItem.File.CheckIn(string.Empty);
                    }


                    Folder.CheckInFilesRecursive(vSPDocumentLibrary.RootFolder, pSPCheckinType);
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(DocumentLibrary).Namespace, typeof(DocumentLibrary).Name, "ChekInFilesRecursive", ex);
                throw;
            }
        }

        public static void ChekInFilesRecursive(string pSiteUrl, string pWebUrl, string pDocumentLibraryTitle, SPCheckinType pSPCheckinType)
        {

            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pDocumentLibraryTitle);

                            if (vSPList != null)
                            {
                                if (vSPList.BaseType == SPBaseType.DocumentLibrary)
                                {
                                    SPDocumentLibrary vSPDocumentLibrary = vSPList as SPDocumentLibrary;
                                    ChekInFilesRecursive(vSPDocumentLibrary, pSPCheckinType);
                                }

                            }
                        }
                    }
                //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(DocumentLibrary).Namespace, typeof(DocumentLibrary).Name, "ChekInFilesRecursive", ex);
                throw;
            }
        }

        public static void RemoveAllItems(SPDocumentLibrary pSPDocumentLibrary)
        {
            try
            {
                if (pSPDocumentLibrary != null && pSPDocumentLibrary.ItemCount > 0)
                {
                    foreach (SPListItem item in pSPDocumentLibrary.Items)
                    {
                        item.File.Delete();
                    }
                }
            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(DocumentLibrary).Namespace, typeof(DocumentLibrary).Name, "RemoveAllItems", ex);
                throw;
            }
        }

        public static void SetBrowserFileHandling(string pSiteUrl, string pWebUrl, string pDocumentLibraryTitle, SPBrowserFileHandling pSPBrowserFileHandling)
        {

            try
            {
                //SPSecurity.RunWithElevatedPrivileges(delegate()
                //{
                    using (SPSite vSPSite = new SPSite(pSiteUrl))
                    {
                        using (SPWeb vSPWeb = vSPSite.OpenWeb(pWebUrl))
                        {

                            SPList vSPList = vSPWeb.Lists.TryGetList(pDocumentLibraryTitle);

                            if (vSPList != null)
                            {
                                if (vSPList.BaseType == SPBaseType.DocumentLibrary)
                                {
                                    SPDocumentLibrary vSPDocumentLibrary = vSPList as SPDocumentLibrary;
                                    if (vSPDocumentLibrary.BrowserFileHandling != pSPBrowserFileHandling)
                                    {
                                        vSPDocumentLibrary.BrowserFileHandling = pSPBrowserFileHandling;
                                        vSPDocumentLibrary.Update();
                                    }
                                }

                            }
                        }
                    }
                //});

            }
            catch (Exception ex)
            {
                Logging.LogMessase(typeof(DocumentLibrary).Namespace, typeof(DocumentLibrary).Name, "SetBrowserFileHandling", ex);
                throw;
            }
        }
    }
}
